package com.aaa.service.impl;

import com.aaa.common.domain.RenterInfo;
import com.aaa.common.domain.RentOutContractCURDInfo;
import com.aaa.common.domain.RentOutContractInfo;
import com.aaa.common.domain.RentReceiveInfo;
import com.aaa.common.result.Result;
import com.aaa.mapper.RentOutContractInfoMapper;
import com.aaa.mapper.RentReceiveInfoMapper;
import com.aaa.service.RentOutContractAddService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 宋文森
 * @Date: 2021/01/30/15:58
 * @Description: 租出合同添加操作服务实现类
 */
@Service
public class RentOutContractAddServiceImpl implements RentOutContractAddService {

    @Autowired
    private RentOutContractInfoMapper rentOutContractInfoMapper;
    @Autowired
    private RentOutContractInfo rentOutContractInfo;
    @Autowired
    private RentReceiveInfo rentReceiveInfo;
    @Autowired
    private RenterInfo renterInfo;
    @Autowired
    private RentReceiveInfoMapper rentReceiveInfoMapper;

    @Autowired
    private OwnAssetsContractServiceImpl ownAssetsContractService;

    @Transactional
    public Result insert(RentOutContractCURDInfo rentOutContractCURDInfo) {
        Date contractBeginDate = rentOutContractCURDInfo.getContractBeginDate();
        Date contractEndDate = rentOutContractCURDInfo.getContractEndDate();
        Date freeBeginDate = rentOutContractCURDInfo.getFreeBeginDate();
        Date freeEndDate = rentOutContractCURDInfo.getFreeEndDate();
        if (contractEndDate.after(contractBeginDate) && freeEndDate.after(freeBeginDate)) {
            /**
             * 先将租户信息添加到租户信息表中并返回一个自增列id
             */
            renterInfo.setId(rentOutContractCURDInfo.getId());
            renterInfo.setContact(rentOutContractCURDInfo.getContact());
            renterInfo.setName(rentOutContractCURDInfo.getName());
            renterInfo.setPhone(rentOutContractCURDInfo.getPhone());
            Integer integer = ownAssetsContractService.insertRenter(renterInfo);
            Integer lesseeId = renterInfo.getId();


            rentOutContractInfo.setContractId(rentOutContractCURDInfo.getContractId());
            rentOutContractInfo.setAssetId(rentOutContractCURDInfo.getAssetId());
            rentOutContractInfo.setRenterType(rentOutContractCURDInfo.getRenterType());
            rentOutContractInfo.setSystemRent(rentOutContractCURDInfo.getSystemRent());
            rentOutContractInfo.setCounterBooth(rentOutContractCURDInfo.getCounterBooth());
            rentOutContractInfo.setRentUse(rentOutContractCURDInfo.getRentUse());
            rentOutContractInfo.setSubletState(rentOutContractCURDInfo.getSubletState());
            rentOutContractInfo.setContractBeginDate(rentOutContractCURDInfo.getContractBeginDate());
            rentOutContractInfo.setContractEndDate(rentOutContractCURDInfo.getContractEndDate());
            rentOutContractInfo.setFreeBeginDate(rentOutContractCURDInfo.getFreeBeginDate());
            rentOutContractInfo.setFreeEndDate(rentOutContractCURDInfo.getFreeEndDate());
            rentOutContractInfo.setGrowthRate(rentOutContractCURDInfo.getGrowthRate());
            rentOutContractInfo.setDeposit(rentOutContractCURDInfo.getDeposit());
            rentOutContractInfo.setContractStatusId(rentOutContractCURDInfo.getContractStatusId());
            rentOutContractInfo.setBusinessStatusId(rentOutContractCURDInfo.getBusinessStatusId());
            rentOutContractInfo.setRemark(rentOutContractCURDInfo.getRemark());
            rentOutContractInfo.setListed(rentOutContractCURDInfo.getListed());
            rentOutContractInfo.setLessor(rentOutContractCURDInfo.getLessor());
            rentOutContractInfo.setRentBuildArea(rentOutContractCURDInfo.getRentBuildArea());
            rentOutContractInfo.setRentSoilArea(rentOutContractCURDInfo.getRentSoilArea());
            rentOutContractInfo.setLesseeId(lesseeId);
            rentOutContractInfo.setAssetTypeId(rentOutContractCURDInfo.getAssetTypeId());
            if (rentOutContractCURDInfo.getRentInContractId() != null) {
                rentOutContractInfo.setRentInContractId(rentOutContractCURDInfo.getRentInContractId());
            }
            int insertRentOutContractInfo = rentOutContractInfoMapper.insert(rentOutContractInfo);
            rentReceiveInfo.setContractId(rentOutContractCURDInfo.getContractId());
            rentReceiveInfo.setAssetId(rentOutContractCURDInfo.getAssetId());
            rentReceiveInfo.setAnnualRent(rentOutContractCURDInfo.getAnnualRent());
            rentReceiveInfo.setPayMethodId(rentOutContractCURDInfo.getPayMethodId());
            rentReceiveInfo.setRentchargeStatusId(rentOutContractCURDInfo.getRentchargeStatusId());
            int insertRentReceiveInfo = rentReceiveInfoMapper.insert(rentReceiveInfo);

            if (insertRentOutContractInfo > 0 && insertRentReceiveInfo > 0 && renterInfo != null) {
                return Result.success("添加成功！");
            }
            return Result.error("添加失败！");
        }


        return Result.error("添加失败！");
    }
}
